Passed
Branch master (b8a2ae)
by Samson
02:23
created

GeezCalculator.js ➔ processToken   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
const Converter = require('../Converter/Converter');
0 ignored issues
show
Unused Code introduced by
The constant Converter seems to be never used. Consider removing it.
Loading history...
2
3
const ONE = 1;
0 ignored issues
show
Unused Code introduced by
The constant ONE seems to be never used. Consider removing it.
Loading history...
4
const HUNDRED = 100;
0 ignored issues
show
Unused Code introduced by
The constant HUNDRED seems to be never used. Consider removing it.
Loading history...
5
const TEN_THOUSAND = 10000;
0 ignored issues
show
Unused Code introduced by
The constant TEN_THOUSAND seems to be never used. Consider removing it.
Loading history...
6
7
/**
8
 * GeezCalculator calculate the ascii number from the parsed queue.
9
 *
10
 * @author Sam As End <4sam21{at}gmail.com>
11
 */
12
module.exports = class GeezCalculator {
13
    constructor($queue) {
14
        this.queue = $queue;
15
        this.total = 0;
16
        this.sub_total = 0;
17
    }
18
19
    /**
20
     * Do the magic.
21
     */
22
    calculate() {
23
        this.resetSubTotalToZero();
24
25
        this.queue.forEach(($token) => {
26
            this.processToken($token);
27
        });
28
    }
29
30
    /**
31
     * set the sub total attribute to zero.
32
     */
33
    resetSubTotalToZero() {
34
        this.sub_total = 0;
35
    }
36
37
    /**
38
     * Process a single token from the Queue.
39
     *
40
     * @param $token
41
     */
42
    processToken($token) {
43
        let $block = $token['block'];
44
        let $separator = $token['separator'];
45
46
        this.processBySeparator($block, $separator);
47
    }
48
49
    /**
50
     * Process based on separator.
51
     *
52
     * @param $block
53
     * @param $separator
54
     */
55
    processBySeparator($block, $separator) {
56
        switch ($separator) {
57
            case ONE:
0 ignored issues
show
Bug introduced by
The variable ONE seems to be never declared. If this is a global, consider adding a /** global: ONE */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
58
                this.addToTotal($block);
59
                break;
60
            case HUNDRED:
0 ignored issues
show
Bug introduced by
The variable HUNDRED seems to be never declared. If this is a global, consider adding a /** global: HUNDRED */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
61
                this.updateSubTotal($block);
62
                break;
63
            case TEN_THOUSAND:
0 ignored issues
show
Bug introduced by
The variable TEN_THOUSAND seems to be never declared. If this is a global, consider adding a /** global: TEN_THOUSAND */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
64
                this.updateTotal($block);
65
                break;
66
        }
67
    }
68
69
    /**
70
     * Add the sub total and the block to total
71
     * and reset sub total to zero.
72
     *
73
     * @param $block
74
     *
75
     * @return void
76
     */
77
    addToTotal($block) {
78
        this.total += (this.sub_total + $block);
79
        this.resetSubTotalToZero();
80
    }
81
82
    /**
83
     * Is the leading block?
84
     *
85
     * @param $block
86
     *
87
     * @return bool
88
     */
89
    isLeading($block) {
90
        return this.isBlockZero($block) &&
91
            this.isSubtotalZero();
92
    }
93
94
    /**
95
     * Is the value of block zero?
96
     *
97
     * @param $block
98
     *
99
     * @return bool
100
     */
101
    isBlockZero($block) {
102
        return this.isZero($block);
103
    }
104
105
    /**
106
     * Is a number zero?
107
     *
108
     * @param $number
109
     *
110
     * @return boolean
111
     */
112
    isZero($number) {
113
        return $number === 0;
114
    }
115
116
    /**
117
     * Is sub total attribute zero?
118
     *
119
     * @return bool
120
     */
121
    isSubtotalZero() {
122
        return this.isZero(this.sub_total);
123
    }
124
125
    /**
126
     * Add number to sun total.
127
     *
128
     * @param $number integer
129
     */
130
    addToSubTotal($number) {
131
        this.sub_total += $number;
132
    }
133
134
    /**
135
     * Is the leading 10k?
136
     *
137
     * @param $block
138
     *
139
     * @return bool
140
     */
141
    isLeadingTenThousand($block) {
142
        return this.isTotalZero() &&
143
            this.isLeading($block);
144
    }
145
146
    /**
147
     * Is the total attribute zero?
148
     *
149
     * @return bool
150
     */
151
    isTotalZero() {
152
        return this.isZero(this.total);
153
    }
154
155
    /**
156
     * Multiply the total attribute by ten thousand.
157
     */
158
    multiplyTotalBy10k() {
159
        this.total *= TEN_THOUSAND;
0 ignored issues
show
Bug introduced by
The variable TEN_THOUSAND seems to be never declared. If this is a global, consider adding a /** global: TEN_THOUSAND */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
160
    }
161
162
    /**
163
     * Return the calculated ascii number.
164
     *
165
     * @return int
166
     */
167
    getCalculated() {
168
        return this.total;
169
    }
170
171
    /**
172
     * Update the sub total attribute.
173
     *
174
     * @param $block
175
     */
176
    updateSubTotal($block) {
177
        if (this.isLeading($block)) {
178
            $block = ONE;
0 ignored issues
show
Bug introduced by
The variable ONE seems to be never declared. If this is a global, consider adding a /** global: ONE */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
179
        }
180
181
        $block *= HUNDRED;
0 ignored issues
show
Bug introduced by
The variable HUNDRED seems to be never declared. If this is a global, consider adding a /** global: HUNDRED */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
182
183
        this.addToSubTotal($block);
184
    }
185
186
    /**
187
     * Update the sub total attribute.
188
     *
189
     * @param $block
190
     */
191
    updateTotal($block) {
192
        if (this.isLeadingTenThousand($block)) {
193
            $block = ONE;
0 ignored issues
show
Bug introduced by
The variable ONE seems to be never declared. If this is a global, consider adding a /** global: ONE */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
194
        }
195
196
        this.addToTotal($block);
197
        this.multiplyTotalBy10k();
198
    }
199
};
200